package com.server.questionbank.snframe.mapper.Course;

import com.server.questionbank.snframe.domain.Course.Chapter.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TChapterMapper {

    // 获取章节列表
    @Select("SELECT T_chapter.* " +
            "FROM T_chapter " +
            "JOIN T_course ON T_chapter.course_id = T_course.course_id " +
            "WHERE T_course.course_id = #{courseId} " +
            "ORDER BY T_chapter.display_order ASC;")
    public List<TChapter> getChapterList(@Param("courseId") Long courseId);

    // 获取章节信息
    @Select("SELECT * " +
            "FROM T_chapter " +
            "WHERE T_chapter.chapter_id = #{chapterId};")
    public TChapter getChapterInfo(@Param("chapterId") Long chapterId);

    // 获取课时列表
    @Select("SELECT T_lessons.* " +
            "FROM T_lessons " +
            "JOIN T_chapter ON T_lessons.chapter_id = T_chapter.chapter_id " +
            "WHERE T_chapter.chapter_id = #{chapterId} " +
            "ORDER BY T_lessons.display_order ASC;")
    public List<TLessons> getLessonList(@Param("chapterId") Long chapterId);

    // 获取课时信息
    @Select("SELECT * " +
            "FROM T_lessons " +
            "WHERE T_lessons.lessons_id = #{lessonsId};")
    public TLessons getLessonInfo(@Param("lessonsId") Long lessonsId);

    // 创建章节与课时
    @Insert("INSERT INTO T_chapter (course_id, chapter_name, display_order, create_by, update_by) " +
            "VALUES (#{courseId}, #{updateChapter.chapterName}, #{updateChapter.displayOrder}, #{createBy}, #{createBy});")
    @Options(useGeneratedKeys = true, keyProperty = "updateChapter.chapterId") // 回写 ID
    public Integer createChapter(@Param("courseId") Long courseId, @Param("updateChapter") UpdateChapter updateChapter,  @Param("createBy") Long createBy);
    @Insert("INSERT INTO T_lessons (chapter_id, lesson_name, display_order, create_by, update_by) " +
            "VALUES (#{updateLesson.chapterId}, #{updateLesson.lessonName}, #{updateLesson.displayOrder}, #{createBy}, #{createBy});")
    @Options(useGeneratedKeys = true, keyProperty = "updateLesson.lessonsId")
    public Integer createLesson(@Param("updateLesson") UpdateLesson updateLesson, @Param("createBy") Long createBy);

    // 更新章节与课时
    @Update("UPDATE T_chapter " +
            "SET chapter_name = #{updateChapter.chapterName}, " +
            "display_order = #{updateChapter.displayOrder}, " +
            "update_by = #{updateBy} " +
            "WHERE chapter_id = #{updateChapter.chapterId};")
    public Integer updateChapter(@Param("updateChapter") UpdateChapter updateChapter, @Param("updateBy") Long updateBy);
    @Update("UPDATE T_lessons " +
            "SET chapter_id = #{updateLesson.chapterId}, " +
            "lesson_name = #{updateLesson.lessonName}, " +
            "display_order = #{updateLesson.displayOrder}, " +
            "update_by = #{updateBy} " +
            "WHERE lessons_id = #{updateLesson.lessonsId};")
    public Integer updateLesson(@Param("updateLesson") UpdateLesson updateLesson, @Param("updateBy") Long updateBy);

    // 删除章节与课时
    @Delete("DELETE FROM T_chapter WHERE chapter_id = #{chapterId};")
    public Integer deleteChapter(@Param("chapterId") Long chapterId);
    @Delete("DELETE FROM T_lessons WHERE lessons_id = #{lessonsId};")
    public Integer deleteLesson(@Param("lessonsId") Long lessonsId);

    // 获取课时资源
    @Select("SELECT * " +
            "FROM lessons_resource " +
            "WHERE lessons_resource.lessons_id = #{lessonsId} " +
            "ORDER BY display_order ASC;")
    public List<LessonsResource> getLessonResource(@Param("lessonsId") Long lessonsId);
    // 获取课时资源信息
    @Select("SELECT * " +
            "FROM lessons_resource " +
            "WHERE lessons_resource.resource_id = #{resourceId};")
    public LessonsResource getLessonResourceInfo(@Param("resourceId") Long resourceId);

    // 创建课时资源
    @Insert("INSERT INTO lessons_resource (lessons_id, resource_type, resource_name, display_order, resource_link, resource_url_name, resource_url, resource_size, duration_seconds) " +
            "VALUES (#{lessonsId}, #{resourceType}, #{resourceName}, #{displayOrder}, #{resourceLink}, #{resourceUrlName}, #{resourceUrl}, #{resourceSize}, #{durationSeconds});")
    public Integer createLessonResource(LessonsResource lessonsResource);

    // 更新课时资源
    @Update("UPDATE lessons_resource " +
            "SET resource_type = #{resourceType}, " +
            "resource_name = #{resourceName}, " +
            "display_order = #{displayOrder}, " +
            "resource_link = #{resourceLink}, " +
            "resource_url_name = #{resourceUrlName}, " +
            "resource_url = #{resourceUrl}, " +
            "resource_size = #{resourceSize}, " +
            "duration_seconds = #{durationSeconds} " +
            "WHERE lessons_resource.resource_id = #{resourceId};")
    public Integer updateLessonResource(LessonsResource lessonsResource);

    // 删除课时资源
    @Delete("DELETE FROM lessons_resource WHERE resource_id = #{resourceId};")
    public Integer deleteLessonResource(@Param("resourceId") Long resourceId);
}
